/**
 * 
 * 地图
 * 
 */
class MapItem extends egret.DisplayObjectContainer {
    /** 初始触摸点 */
    private startPoint: egret.Point = new egret.Point();
    /** 地图容器, 用来拼装地图切片 */
    private map_container: egret.DisplayObjectContainer;

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        this.drawMap();

        this.touchEnabled = true;
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchStart, this);
        this.addEventListener(egret.TouchEvent.TOUCH_END, this.touchEnd, this);
    }

    private drawMap() {
        for (let i = 1; i < 8; i++) {
            const mapSlice = Utils.createBitmapByName(`map_slice_${i}_png`);
            mapSlice.y = this.height;
            this.addChild(mapSlice);
        }
    }

    private touchStart(evt: egret.TouchEvent) {
        this.startPoint.y = evt.stageY;
        this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMove, this);
    }

    private touchEnd() {
        this.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMove, this);
    }

    private touchMove(evt: egret.TouchEvent) {
        /** 地图最大高度 */
        const mapMaxY = -(this.height - this.stage.stageHeight);
        /** 计算拖动距离 */
        let computedY = evt.stageY - this.startPoint.y;
        /** 重置初始位置 */
        this.startPoint.y = evt.stageY;

        if (this.y <= mapMaxY - computedY && computedY < 0) {
            computedY = 0;
        }

        if (this.y >= 0 - computedY && computedY > 0) {
            computedY = 0;
        }

        this.y += computedY;
    }



}